﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace LearnDrive
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();

            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN", true) { DateTimeFormat = { ShortDatePattern = "yyyy-MM-dd", FullDateTimePattern = "yyyy-MM-dd HH:mm:ss", LongTimePattern = "HH:mm:ss" } };

            string mFilePath = Application.StartupPath + "\\AutoUpdate.exe";
            if (File.Exists(mFilePath))
                Process.Start(mFilePath);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text;
            string pass = this.txtPwd.Text;
            LoginServer(name, pass);

            
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {            
            //this.tsslQQ.Text = "客服QQ：" + Myinfo.QQ;
            this.tsslPhone.Text = "加盟热线：" + Myinfo.phone;
            List<string> source = AShelp.Load("user");
            if (source != null)
            {
                this.txtName.Text = source[0];
                this.txtPwd.Text = source[1];
            }
            this.Text = "大拇指驾考交规速成-" + Myinfo.version ;
           
        }


        private void LoginServer(string name, string pass)
        {
            string key = "AutoSend";
            string iv = "weiwei@1";
            string[] ips = AShelp.GetIP();
            //strPass格式：name|pass|blongSys_id|当前时间
            //blongSys_id要和数据库表belongSys中的blongSys_id对应
            
            //ips[1] = "山东省聊城市 联通";
            string strPass = name + "|" + pass + "|1|" + DateTime.Now+"|"+ips[0]+"|"+ips[1]+"|";

            string md5strPass = Tools.Encode(strPass, key, iv);

            //HeartWebService.HeartSoapClient hwm = new HeartWebService.HeartSoapClient();
            WebReference2.Heart hwm = new WebReference2.Heart();
            try
            {

                string re = "-1";
                int isProbation = 0;
                string nowtime = "";

                string result = hwm.Login(md5strPass);

                if (!string.IsNullOrEmpty(result))
                {
                    //格式为：
                    //1.    result|服务器时间，
                    //      result：-1、用户名密码错误，-2、此用户在其他地方登录，-3、用户无授权，-4、用户被冻结
                    //
                    //2.    result|isProbation|服务器时间，
                    //      result：到期时间
                    //      isProbation：是否试用，0、否，1、是
                    //3.    result|isProbation|服务器时间|身份号|user_name|，
                    //      result：到期时间
                    //      isProbation：是否试用，0、否，1、是

                    result = Tools.Decode(result, key, iv);

                    string[] r = result.Split('|');

                    if (r.Length == 2)
                    {
                        re = r[0];
                    }
                    if (r.Length == 3)
                    {
                        re = r[0];
                        isProbation = Convert.ToInt32(r[1]);
                        nowtime = r[2];
                    }
                    if (r.Length == 6)
                    {
                        re = r[0];
                        isProbation = Convert.ToInt32(r[1]);
                        nowtime = r[2];
                        if (r[3].Trim().Length != 0)
                            Myinfo.SNO = r[3];
                        else
                            Myinfo.SNO = "372501199901010111";
                        if (r[4].Trim().Length != 0)
                            Myinfo.usertruename = r[4];
                        else
                            Myinfo.usertruename = name;


                    }


                }

                if (re == "-1")//
                {
                    MessageBox.Show("用户名密码错误！");
                    return;
                }
                if (re == "-2")
                {
                    MessageBox.Show("此用户已登录,无法重复登陆!");
                    return;
                }
                if (re == "-3")
                {
                    MessageBox.Show("此用户无授权！");
                    return;
                }
                if (re == "-4")
                {
                    MessageBox.Show("此用户被冻结！");
                    return;
                }

                if (isProbation == 1)
                {
                    if (re.ToString() == nowtime)
                    {
                        MessageBox.Show("试用期已到期！");

                    }
                    else
                    {
                        //MessageBox.Show("欢迎试用本软件！");
                        //Myinfo.days = re;
                        DateTime n = DateTime.Now;
                        DateTime.TryParse(nowtime, out n);
                        DateTime s = DateTime.Now;
                        DateTime.TryParse(re, out s);
                        Myinfo.softtime = s - n;
                        if (ckbremenber.Checked)
                        {
                            AShelp.Save(this.txtName.Text, this.txtPwd.Text, "", "", "user");
                            Myinfo.username = this.txtName.Text;
                        }
                        if (Myinfo.softtime.Seconds > 0)
                        {
                            MessageBox.Show("您的软件使用期限还有" + AShelp.DateDiff(ref Myinfo.softtime));
                            frmMain f = new frmMain(name, pass);
                            f.Show();
                            //Myinfo.myfmain = f;
                            this.Hide();
                        }
                        else
                        {
                            frmMsg f = new frmMsg("软件已到期,请联系软件公司！", 5, true);
                            f.ShowDialog();
                            Application.ExitThread();
                        }
                    }



                }
                else
                {
                    if (re.ToString() == nowtime)
                    {
                        MessageBox.Show("授权已到期！");
                    }
                    else
                    {
                        //Myinfo.days = re;
                        DateTime n = DateTime.Now;
                        DateTime.TryParse(nowtime, out n);
                        DateTime s = DateTime.Now;
                        DateTime.TryParse(re, out s);
                        Myinfo.softtime = s - n;
                        if (ckbremenber.Checked)
                        {
                            AShelp.Save(this.txtName.Text, this.txtPwd.Text, "", "", "user");
                            Myinfo.username = this.txtName.Text;
                        }
                        if (Myinfo.softtime.Seconds > 0)
                        {
                            MessageBox.Show("您的软件使用期限还有" + AShelp.DateDiff(ref Myinfo.softtime));
                            frmMain f = new frmMain(name, pass);
                            f.Show();
                            //Myinfo.myfmain = f;
                            this.Hide();
                        }
                        else
                        {
                            frmMsg f = new frmMsg("软件已到期,请联系软件公司！", 5, true);
                            f.ShowDialog();
                            Application.ExitThread();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器未连接成功！");
            }
        }
    }
}
